package com.zgg.handle

import ExcelMessageDao
import com.zgg.core.annotation.HttpHandler
import com.zgg.core.annotation.Param
import io.vertx.core.json.JsonArray
import io.vertx.core.json.JsonObject
import io.vertx.ext.web.FileUpload
import io.vertx.ext.web.RoutingContext
import org.apache.poi.openxml4j.opc.OPCPackage
import org.apache.poi.xssf.usermodel.XSSFWorkbook

/**
 * @author: zgg
 * @date: 2021-07-03 23:29
 */
@HttpHandler(path = "/main")
class TestHandle {
    @HttpHandler(path = "/int", produce = "text/plain; charset=utf-8")
    fun hi(@Param msg: Int?): String? {
        println("测试成功$msg")
        return "hi $msg"
    }

    @HttpHandler(path = "/name", produce = "application/json; charset=utf-8")
    fun test(msg: String, aa: String): JsonObject? {
        println("测试成功$msg $aa")
        return JsonObject().put("hello", "world")
    }

    @HttpHandler(path = "/uploads", produce = "text/plain; charset=utf-8")
    suspend fun uploadFile(it: RoutingContext): String {
        val uploads: Set<FileUpload> = it.fileUploads()
        if (uploads.isNotEmpty() && uploads.size == 1) {
            for (upload in uploads) {
                println("文件名称： ${upload.fileName()}")
                println("文件大小：${upload.size()}")
                return "成功保存数据${printFile(upload)}条"
            }
        }
        return "未找到可保存文件"
    }

    private suspend fun printFile(upload: FileUpload): Int {
        val excelMessageDao = ExcelMessageDao();
        val opcPackage: OPCPackage = OPCPackage.open(upload.uploadedFileName())
        val workbook = XSSFWorkbook(opcPackage)
        val sheet0 = workbook.getSheetAt(0)
        val jsonArray = JsonArray()
        for (rowIndex in 3 until sheet0.lastRowNum) {
            val row = sheet0.getRow(rowIndex)
            val jsonObject = JsonObject()
            //jsonObject.put("_id", "gwy_${System.currentTimeMillis()}_${rowIndex}")
            for (cellIndex in 0..row.lastCellNum) {
                when (cellIndex) {
                    0 -> jsonObject.put("zljg", row.getCell(cellIndex).toString())
                    1 -> jsonObject.put("jgxz", row.getCell(cellIndex).toString())
                    2 -> jsonObject.put("jgcj", row.getCell(cellIndex).toString())
                    3 -> jsonObject.put("zflb", row.getCell(cellIndex).toString())
                    4 -> jsonObject.put("zwmc", row.getCell(cellIndex).toString())
                    5 -> jsonObject.put("zjcc", row.getCell(cellIndex).toString())
                    6 -> jsonObject.put("zwdm", row.getCell(cellIndex).toString())
                    7 -> jsonObject.put("zlrs", row.getCell(cellIndex).toString())
                    8 -> jsonObject.put("zy", row.getCell(cellIndex).toString())
                    9 -> jsonObject.put("xl", row.getCell(cellIndex).toString())
                    10 -> jsonObject.put("xw", row.getCell(cellIndex).toString())
                    11 -> jsonObject.put("xb", row.getCell(cellIndex).toString())
                    12 -> jsonObject.put("age", row.getCell(cellIndex).toString())
                    13 -> jsonObject.put("jlyq", row.getCell(cellIndex).toString())
                    14 -> jsonObject.put("other", row.getCell(cellIndex).toString())
                    15 -> jsonObject.put("sllb", row.getCell(cellIndex).toString())
                    16 -> jsonObject.put("zykm", row.getCell(cellIndex).toString())
                    17 -> jsonObject.put("remark", row.getCell(cellIndex).toString())
                    18 -> jsonObject.put("zxdh", row.getCell(cellIndex).toString())
                }
            }
            jsonArray.add(jsonObject)
        }
        return excelMessageDao.saveJsonArrayExec("datas", jsonArray)
    }

}